﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using TodoApp.TMS_Basics.TMS_line;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_line
{
    public class TMS_lines : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_lines
    {
        public TMS_lines(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 线路显示
        public async Task<PageResponse<TMS_linesDto>> Getline(string LinName, string PeopName,string PeopPhone,string PeopDds, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_linesDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_lines where 1=1 ";
            if (!string.IsNullOrEmpty(LinName))
            {
                sql += $" and LinName like '%{LinName}%'";
            }
            if (!string.IsNullOrEmpty(PeopName))
            {
                sql += $" and PeopName  like '%{PeopName}%'";
            }
            if (!string.IsNullOrEmpty(PeopPhone))
            {
                sql += $" and PeopPhone  like '%{PeopPhone}%'";
            }
            if (!string.IsNullOrEmpty(PeopDds))
            {
                sql += $" and PeopDds  like '%{PeopDds}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_linesDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_linesDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 线路添加
        public async Task<int> LinsAdds(TMS_linesDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_Lines values ('{info.Id}','{info.LinName}','{info.StartPont}','{info.EndPont}','{info.PeopName}','{info.PeopPhone}','{info.PeopDds}','{info.LinText}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 线路删除
        public async Task<int> Deletline(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_Lines where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

    }
}
